package sibu.cn.mydemo.customview.surfaceview;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

/**
 * TODO: document your custom view class.
 */
public class MySurfaceView extends SurfaceView  implements SurfaceHolder.Callback,Runnable{

    private SurfaceHolder surfaceHolder;
    private Paint paint;
    private Thread th;
    private Canvas canvas;

    public MySurfaceView(Context context) {
        super(context);
        init(null, 0);
    }

    public MySurfaceView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(attrs, 0);
    }

    public MySurfaceView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init(attrs, defStyle);
    }

    private void init(AttributeSet attrs, int defStyle) {
        // Load attributes
        surfaceHolder = this.getHolder();
        surfaceHolder.addCallback(this);
        paint = new Paint();
        paint.setColor(Color.BLACK);


    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        th = new Thread(this);
        th.start();
    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {

    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {

    }

    public void myDraw(){

        canvas = surfaceHolder.lockCanvas();

        surfaceHolder.unlockCanvasAndPost(canvas);

    }

    @Override
    public void run() {
        myDraw();
    }
}
